package com.lbtc.controller;

import com.lbtc.common.ApiRestResponse;
import com.lbtc.exception.LbtcMallException;
import com.lbtc.filter.UserFilter;
import com.lbtc.service.CartService;
import com.lbtc.service.Impl.CartServiceImpl;
import com.lbtc.vo.CartVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/cart")
public class CartController {

    @Autowired
    private CartService cartService;
    @PostMapping("/add")
    public ApiRestResponse addCart(@RequestParam Integer productId, @RequestParam Integer count) throws LbtcMallException {
        //定义用户过滤器来完成获取用户信息，完成用户校验
        //测试用户过滤器是否生效
        //内部获取用户ID，防止横向越权
        //Integer userId = UserFilter.currentUser.getId();
        List<CartVO> list = cartService.add(UserFilter.currentUser.getId(), productId, count);
        return ApiRestResponse.success(list);

    }

    //购物车列表
    @GetMapping("/list")
    public ApiRestResponse listCart() {
        //内部获取用户ID，防止横向越权
        //Integer userId = UserFilter.currentUser.getId();
        List<CartVO> list = cartService.list(UserFilter.currentUser.getId());
        return ApiRestResponse.success(list);
    }

    @PostMapping("/update")
    @ApiOperation("更新购物车")
    public ApiRestResponse update(@RequestParam Integer productId, @RequestParam Integer count) throws LbtcMallException {
        List<CartVO> cartVOList = cartService.update(UserFilter.currentUser.getId(), productId, count);
        return ApiRestResponse.success(cartVOList);
    }

    @PostMapping("/delete")
    @ApiOperation("删除购物车")
    public ApiRestResponse delete(@RequestParam Integer productId) throws LbtcMallException {
        //不能传入userID，cartID，否则可以删除别人的购物车
        List<CartVO> cartVOList = cartService.delete(UserFilter.currentUser.getId(), productId);
        return ApiRestResponse.success(cartVOList);
    }

    @PostMapping("/select")
    @ApiOperation("选择/不选择购物车的某商品")
    public ApiRestResponse select(@RequestParam Integer productId, @RequestParam Integer selected) throws LbtcMallException {
        //全选和单选是有相似点的，因此全选功能可以复用单选功能
        List<CartVO> cartVOList = cartService.selectOrNot(UserFilter.currentUser.getId(), productId, selected);
        return ApiRestResponse.success(cartVOList);
    }
    @PostMapping("/selectAll")
    @ApiOperation("全选择/全不选择购物车的某商品")
    public ApiRestResponse selectAll(@RequestParam Integer selected) {
        //不能传入userID，cartID，否则可以删除别人的购物车
        List<CartVO> cartVOList = cartService.selectAllOrNot(UserFilter.currentUser.getId(), selected);
        return ApiRestResponse.success(cartVOList);
    }
}
